<?php
//http://localhost/app/index.php?i=1&c=entry&do=scan&m=xdian
/**
 * Created by PhpStorm.
 * User: SnGef
 * Date: 2017/6/23
 * Time: 18:50
 */
global $_W, $_GPC;
$qrCode = empty($_GPC['qrCode']) ? "" : $_GPC['qrCode'];

if (!empty($qrCode)) {
    $duration = empty($_GPC['duration']) ? "1" : $_GPC['duration'];

    $deviceInfo = pdo_fetch("SELECT a.*,b.group_name,b.price FROM " . tablename('xd_device') . " a LEFT JOIN " . tablename('xd_device_group') . " b on a.device_group_id=b.id WHERE a.id=:id ",
        array(
            ':id' => $qrCode
        )
    );

    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
    $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    $price = $deviceInfo['price'];
    $amount= floatval($duration) * floatval($price);
    $scan_time = time();
    $scanInfo = [
        'uid' => $_W['uniaccount']['uid'],
        'device_id' => $deviceInfo['id'],
        'scan_time' => $scan_time,
        'duration' => $duration,
        'price' => $deviceInfo['price'],
        'scan_sn' => $orderSn
    ];
    $result = pdo_insert('xd_scan_record', $scanInfo);

    if(!empty($result)) {
        //构建支付请求

        $payParams = array(
            'ordersn' => $orderSn,  //收银台中显示的订单号
            'title' => '充电',          //收银台中显示的标题
            'fee' => $amount,      //收银台中显示需要支付的金额,只能大于 0
        );

        //实例化，并获取支付提交参数
        $wechatPay=new WechatPay();
        $jsApiParameters = $wechatPay->XdianWechatPay($payParams);
        include  $this->template('wechat_pay');






    }else{
        include $this->template('scan');
    }


//    $memberInfo = pdo_fetch("SELECT * FROM " . tablename('mc_mapping_fans') . " WHERE  uid = :id ",
//        array(
//            ':id' => $_W['uniaccount']['uid']
//        )
//    );

//    $deviceInfo = pdo_fetch("SELECT a.*,b.group_name,b.price FROM ". tablename('xd_device') . " a LEFT JOIN ". tablename('xd_device_group') . " b on a.device_group_id=b.id WHERE a.id=:id ",
//        array(
//            ':id' => $qrCode
//        )
//    );
//
//
////    print_r($memberInfo);
////    print_r($deviceInfo);
////    exit();
//    $create_time=time();
//    $deviceUseInfo=[
//        'uid' => $memberInfo['uid'],
//        'device_id' => $deviceInfo['id'],
//        'create_time' =>$create_time,
//        'code' => '123456',
//        'duration' => $duration,
//        'price' => $deviceInfo['price']
//    ];


} else {
    include $this->template('scan');
}
